﻿using LCA.Community.DAL;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using LCA.Community.DAL.Model;
using LCA.Community.DAL.Interfaces;
using System.Collections.Generic;
using LCA.Community.DAL.Test.ModelFactories;
using Microsoft.Practices.Unity;

namespace LCA.Community.DAL.Test
{
    /// <summary>
    ///This is a test class for AnswersVotesRepositoryTest and is intended
    ///to contain all AnswersVotesRepositoryTest Unit Tests
    ///</summary>
    [TestClass()]
    public class AnswersVotesRepositoryTest : BaseTestClass
    {
        private UsersFactory _usersFactory = new UsersFactory();
        private QuestionsFactory _questionsFactory = new QuestionsFactory();
        private AnswersFactory _answersFactory = new AnswersFactory();
        private AnswerVotesFactory _answersVoteFactory = new AnswerVotesFactory();
        private Question _question;
        private Answer _answer;
        private List<User> _users;
        private List<AnswerVote> _votes;

        /// <summary>
        /// Initializes the test.
        /// </summary>
        [TestInitialize()]
        public void InitializeTest()
        {
            UnityContainerInitializer();

            _users = _usersFactory.CreateList(5);
            _question = _questionsFactory.CreateEntity(_users[0].RowKey);
            _answer = _answersFactory.CreateEntity(_question.RowKey,_users[0].RowKey);

            var usersRepository = _unityContainer.Resolve<IUsersRepository>();
            _users.ForEach(user => usersRepository.Add(user));

            var questionsRepository = _unityContainer.Resolve<IQuestionsRepository>();
            questionsRepository.Add(_question);

            var answersRepository = _unityContainer.Resolve<IAnswersRepository>();
            answersRepository.Add(_answer);

            var answersVotesRepository = _unityContainer.Resolve<IAnswersVotesRepository>();

            _votes = new List<AnswerVote>();
            _votes.Add(_answersVoteFactory.CreatePositiveVote(_users[1].RowKey, _answer.RowKey));
            _votes.Add(_answersVoteFactory.CreatePositiveVote(_users[2].RowKey, _answer.RowKey));
            answersVotesRepository.Add(_votes[0]);
            answersVotesRepository.Add(_votes[1]);
        }

        /// <summary>
        /// Cleans up the test.
        /// </summary>
        [TestCleanup()]
        public void CleanupTest()
        {
            var answersRepository = _unityContainer.Resolve<IAnswersRepository>();
            var questionsRepository = _unityContainer.Resolve<IQuestionsRepository>();
            var usersRepository = _unityContainer.Resolve<IUsersRepository>();

            answersRepository.DeleteTestEntities();
            questionsRepository.DeleteTestEntities();
            usersRepository.DeleteTestEntities();
        }  

        /// <summary>
        ///A test for DeleteAllAnswerVotes
        ///</summary>
        [TestMethod()]
        public void DeleteAllAnswerVotesTest()
        {
            AnswersVotesRepository target = _unityContainer.Resolve<AnswersVotesRepository>();
            string answerID = _answer.RowKey;
            target.DeleteAllAnswerVotes(answerID);

            var actual = target.GetAnswerVotesByAnswer(answerID);
            CollectionAssert.AreEquivalent(new List<AnswerVote>(), actual);
        }

        /// <summary>
        ///A test for DeleteAllUserVotes
        ///</summary>
        [TestMethod()]
        public void DeleteAllUserVotesTest()
        {
            AnswersVotesRepository target = _unityContainer.Resolve<AnswersVotesRepository>();
            string userID = _users[1].RowKey; 
            target.DeleteAllUserVotes(userID);

            var actual = target.GetAnswerVotesByUser(userID);
            CollectionAssert.AreEquivalent(new List<AnswerVote>(), actual);
        }

        /// <summary>
        ///A test for GetAnswerVotesByAnswer
        ///</summary>
        [TestMethod()]
        public void GetAnswerVotesByAnswerTest()
        {
            AnswersVotesRepository target = _unityContainer.Resolve<AnswersVotesRepository>();
            string answerID = _answer.RowKey; 
            List<AnswerVote> expected = _votes; 
            List<AnswerVote> actual;

            actual = target.GetAnswerVotesByAnswer(answerID);
            CollectionAssert.AreEquivalent(expected, actual);
        }

        /// <summary>
        ///A test for GetAnswerVotesByUser
        ///</summary>
        [TestMethod()]
        public void GetAnswerVotesByUserTest()
        {
            AnswersVotesRepository target = _unityContainer.Resolve<AnswersVotesRepository>();
            string userID = _users[1].RowKey;
            List<AnswerVote> expected = new List<AnswerVote> { _votes[0] };
            List<AnswerVote> actual;

            actual = target.GetAnswerVotesByUser(userID);
            CollectionAssert.AreEquivalent(expected, actual);
        }

        /// <summary>
        ///A test for HasUserVoted
        ///</summary>
        [TestMethod()]
        public void HasUserVotedTest()
        {
            var target = _unityContainer.Resolve<AnswersVotesRepository>();
            string userID = _users[1].RowKey;
            string answerID = _answer.RowKey;

            Assert.IsTrue(target.HasUserVoted(userID, answerID));

            userID = _users[0].RowKey;
            Assert.IsFalse(target.HasUserVoted(userID, answerID));
        }
    }
}
